package com.example.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.course.bean.Topic;
import com.example.course.converter.TopicConverter;
import com.example.course.dto.TopicDTO;
import com.example.course.mapper.TopicMapper;
import com.example.course.service.TopicService;
import com.example.course.vo.TopicVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Jasmine-drs
 * @description 针对表【topic(题库表)】的数据库操作Service实现
 * @createDate 2025-04-22 15:36:40
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic>
        implements TopicService {

    private final TopicConverter topicConverter;

    public TopicServiceImpl(TopicConverter topicConverter) {
        this.topicConverter = topicConverter;
    }

    @Override
    public TopicVO addTopic(TopicDTO topicDTO, boolean isTeacher) {
        if (!isTeacher) {
            throw new RuntimeException("只有教师才能添加题目");
        }

        Topic topic = topicConverter.toEntity(topicDTO);
        this.save(topic);
        return topicConverter.toVO(topic);
    }

    @Override
    public boolean deleteTopic(Integer id, boolean isTeacher) {
        if (!isTeacher) {
            throw new RuntimeException("只有教师才能删除题目");
        }

        return this.removeById(id);
    }

    @Override
    public List<TopicVO> getTopicList() {
        LambdaQueryWrapper<Topic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Topic::getCreateTime);
        List<Topic> topics = this.list(queryWrapper);
        return topicConverter.toVOList(topics);
    }

    @Override
    public TopicVO getTopicById(Integer id) {
        Topic topic = this.getById(id);
        return topicConverter.toVO(topic);
    }
}




